#include "qkeyboardbuttonhost.h"

#include "../qhostfactory.h"
#include "../qpropertyfactory.h"
#include "../property/qabstractproperty.h"
#include "../qcommonstruct.h"

#include <QPushButton>

QKeyboardButtonHost::QKeyboardButtonHost(QAbstractHost *parent):
    QAbstractButtonHost(parent)
{
    setProperty("edit_property","");
    setProperty("need_frame",false);
    setProperty("accept_drop",false);
}

QString QKeyboardButtonHost::get_show_name()
{
    return tr("Keyboard Button");
}

QString QKeyboardButtonHost::get_show_group()
{
    return tr("Buttons");
}

QString QKeyboardButtonHost::get_show_icon()
{
    return ":/images/pushbutton.png";
}

void QKeyboardButtonHost::create_object()
{
    m_object=new QPushButton();
    m_object->setObjectName("keyboardbutton");
}

void QKeyboardButtonHost::init_property()
{
    QAbstractButtonHost::init_property();

    remove_property("objectName");
    remove_property("focusPolicy");
    remove_property("toolTip");
    remove_property("mousePress");
    remove_property("mouseMove");
    remove_property("mouseRelease");
    remove_property("doubleClick");
    remove_property("text");
    remove_property("checkable");
    remove_property("checked");
    remove_property("clicked");

    QAbstractProperty *pro;

    pro=QPropertyFactory::create_property("Enum");
    if(pro!=NULL)
    {
        pro->set_property("name","key");
        pro->set_attribute("show_name",tr("Key"));
        pro->set_attribute("group","Attributes");
        tagComboItem item;
        ComboItems  items;

        for(int i=0;i<26;i++)
        {
            item.m_value=Qt::Key_A+i;
            item.m_text=QString('A'+i);
            items.append(item);
        }

        for(int i=0;i<10;i++)
        {
            item.m_value=Qt::Key_0+i;
            item.m_text=QString('0'+i);
            items.append(item);
        }

        item.m_value=Qt::Key_Shift;
        item.m_text="Shift";
        items.append(item);

        item.m_value=Qt::Key_CapsLock;
        item.m_text="Caps";
        items.append(item);

        item.m_value=Qt::Key_Backspace;
        item.m_text="Backspace";
        items.append(item);

        pro->set_attribute("items",QVariant::fromValue<ComboItems>(items));
        m_object->setProperty("key",Qt::Key_A);
        insert_property(pro,2);

        connect(pro,SIGNAL(refresh()),this,SLOT(key_changed()));
    }

    set_property_value("geometry",QRect(0,0,25,25));
    m_object->setProperty("text","A");
}

void QKeyboardButtonHost::key_changed()
{
    QAbstractProperty *pro=(QAbstractProperty*)sender();

    ComboItems items=pro->get_attribute("items").value<ComboItems>();
    foreach(tagComboItem item,items)
    {
        if(item.m_value==pro->get_value())
        {
            m_object->setProperty("text",item.m_text);
            m_object->setProperty("objectName","Key_"+item.m_text);
            return;
        }
    }
    m_object->setProperty("text","");
    m_object->setProperty("objectName","");
}

qRegisterHost("buttons.keyboardbutton",QKeyboardButtonHost,1000)
